package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IntegralExchangRequest;
import com.sc.nft.controller.request.IntegralPageRequest;
import com.sc.nft.controller.request.ProductDetailsRequest;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.IntegralExchangeService;
import com.sc.nft.service.UserAssetsService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/integral")
@Api(tags = "积分兑换")
public class IntegralExchangeController {
    private final IntegralExchangeService integralExchangeService;
    private final Redisson redisson;
    private final UserAssetsService userAssetsService;

    @ApiOperation(value = "查看积分", notes = "查看积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/findIntegral", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<FindIntegralVO> findIntegral() {
        return Result.success(integralExchangeService.findIntegral());
    }

    @ApiOperation(value = "查看积分兑换列表", notes = "查看积分兑换列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getIntegralExchangeList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    @Deprecated
    public Result<Page<IntegralExchangeProductVO>> getIntegralExchangeList(@Valid @RequestBody PageRequest pageRequest) {
        Page<IntegralExchangeProductVO> page = integralExchangeService.getIntegralExchangeList(pageRequest.getPageNo(), pageRequest.getPageSize());
        return Result.success(page);
    }

    @ApiOperation(value = "查看积分兑换列表V_3.5.0", notes = "查看积分兑换列表V_3.5.0", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getIntegralExchangeList/v3.5.0", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<IntegralExchangeProductVO> getIntegralExchangeListV350(@Valid @RequestBody PageRequest pageRequest) {
        Page<IntegralExchangeProductVO> page = integralExchangeService.getIntegralExchangeList(pageRequest.getPageNo(), pageRequest.getPageSize());
        return PageResult.success(page);
    }

    @ApiOperation(value = "查看积分兑换列表v350", notes = "查看积分兑换列表v350", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getIntegralExchangeList/v.3.5.0", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<IntegralExchangeProductVO> getIntegralExchangeList350(@Valid @RequestBody PageRequest pageRequest) {
        Page<IntegralExchangeProductVO> page = integralExchangeService.getIntegralExchangeList(pageRequest.getPageNo(), pageRequest.getPageSize());
        return PageResult.success(page);
    }

    @ApiOperation(value = "积分兑换数字藏品详情", notes = "积分兑换数字藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getIntegralProductDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<IntegralProductDetailsVO> getIntegralProductDetails(@Valid @RequestBody ProductDetailsRequest productDetailsRequest) {
        IntegralProductDetailsVO integralProductDetails = integralExchangeService.getIntegralProductDetails(productDetailsRequest.getProductId());
        return Result.success(integralProductDetails);
    }

    @ApiOperation(value = "积分兑换盲盒详情", notes = "积分兑换盲盒详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getIntegralBlindBoxDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<IntegralBoxDetailsVO> getIntegralBlindBoxDetails(@Valid @RequestBody ProductDetailsRequest productDetailsRequest) {
        IntegralBoxDetailsVO integralBoxDetailsVO = integralExchangeService.getIntegralBlindBoxDetails(productDetailsRequest.getProductId());
        return Result.success(integralBoxDetailsVO);
    }

    @ApiOperation(value = "兑换商品", notes = "兑换商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/integralExchange", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result integralExchange(@Valid @RequestBody IntegralExchangRequest integralExchangRequest) throws GlobalException {
        RLock lock = redisson.getLock(RedisKeyEnum.USET_ID_LOCK.getKey() + UserPool.getUser().getId());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                integralExchangeService.integralExchange(integralExchangRequest.getProductId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalException(ErrorCode.LATER_TRY);
        } finally {
            lock.unlock();
        }

        return Result.success();
    }

    @ApiOperation(value = "用户兑换记录", notes = "用户兑换记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getUserExchangeRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<UserExchangeRecordVo>> getUserExchangeRecord() {
        return Result.success(integralExchangeService.getUserExchangeRecord());
    }

    @ApiOperation(value = "积分详情", notes = "积分详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/integralDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<IntegralDetailsVO> integralDetails(@Valid @RequestBody PageRequest request) {
        Page<IntegralDetailsVO> page = userAssetsService.integralDetails(request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }


    @ApiOperation(value = "积分详情v3.5.0", notes = "积分详情v3.5.0", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/integralDetails/v3.5.0", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<IntegralDetailsVO> integralDetailsV350(@Valid @RequestBody IntegralPageRequest request) {
        Page<IntegralDetailsVO> page = userAssetsService.integralDetailsByType(UserPool.getUser(), request.getType(), request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }

}
